package com.unytech.project.service.system.impl;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unytech.project.common.Globals;
import com.unytech.project.entity.system.Permission;
import com.unytech.project.entity.system.PermissionGroup;
import com.unytech.project.entity.system.User;
import com.unytech.project.service.base.BaseDao;
import com.unytech.project.service.system.UserService;
import com.unytech.project.utils.MD5Util;

@Service 
@Transactional
public class UserServiceImpl extends BaseDao<User> implements UserService {

	
	public User loginVerify(String username, String password) throws Exception
	{		
		User user = null;
		MD5Util MD5 = new MD5Util();	
		String pwd = (password.length()>Integer.parseInt(Globals.MAXPASSWORD))?password:MD5.getMD5ofStr(password);
		Query query = em.createQuery("select o from User o where o.username=:name and o.password=:pwd");
		query.setParameter("name", username).setParameter("pwd", pwd);
		try {
			user = (User) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return user;
	}

	public List<Permission> getUserPermission(User user)
	{		
		User curuser = em.find(User.class, user.getUserid());

		PermissionGroup permgroup = curuser.getPermgroup();
		List<Permission> permission = (List<Permission>) permgroup.getPermissions();	
		
		return permission;
		
	}

}
